package com.biao.mall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biao.common.utils.PageUtils;
import com.biao.common.utils.Query;
import com.biao.mall.order.dao.OrderItemDao;
import com.biao.mall.order.entity.OrderItemEntity;
import com.biao.mall.order.service.OrderItemService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@RabbitListener(queues = "java.hello.queue")
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }


    @RabbitHandler
//    @RabbitListener(queues = "java.hello.queue")
    public void receiveMessage(Object obj, Channel channel, Message message) {
        System.out.println("内容:" + obj);
        System.out.println("类型" + obj.getClass());
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            channel.basicAck(deliveryTag, false);//手动确定签收


            //deliveryTag,multiple,requeue
            //requeue为false则直接丢弃,true重新进入mq的队列
            //multiple只拒绝当前标签的消息
            channel.basicNack(deliveryTag,false,true);//手动拒收
            System.out.println("收货成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}